<template>
	<view>
		<view class="header">
			<text @click="back" class="iconfont icon-xiangzuojiantou">用药人管理</text>
		</view>
		<view class="item" v-for="v in itemData" :key="v.yyr_id" @click="selectedItem(v.yyr_id)">
			<view class="cFirst">
				<view>
					<view>{{v.yyr_name}} <text class="iconfont icon-yishiming1-copy"></text><text :class="v.yyr_id==moren?'iconfont icon-jianqu':''"></text></view>
					<!-- <text class="iconfont icon-xinyongbaogao-benren-copy"></text> -->
					<view>{{v.yyr_sex}} {{v.yyr_age}}岁  {{v.yyr_tel}}</view>
				</view>
				<text class="iconfont icon-yijianfankui" @click.stop="edit(v.yyr_id)"></text>
			</view>
		</view>
		<view class="btn">
			<button type="warn" @click="add">新增用药人</button>
		</view>
	</view>
</template>

<script>
	import {reactive,toRefs,computed,onMounted} from 'vue';
	import {useStore} from 'vuex'
	import http from '../../utils/http.js';
	export default {
		setup(){
			const {state,getters,commit,dispatch} = useStore();
			let data=reactive({
				itemData:computed(()=>state.shopcart.yongyaorenArr),
				moren:1,
				back(){
					uni.redirectTo({
						url:'/pages/addchufang/addchufang'
					})
				},
				edit(id){
					// console.log('编辑',id)
					for (let i = 0; i < data.itemData.length; i++) {
						if(data.itemData[i].yyr_id==id){
							commit('shopcart/setupdateyyr',data.itemData[i])
						}
					}
					// console.log(state.shopcart.updateyyr)
					uni.navigateTo({
						url: '/pages/edityongyaoren/edityongyaoren'
					});
				},
				add(){
					uni.navigateTo({
						url:'/pages/addyongyaoren/addyongyaoren'
					})
				},
				// 选中itme
				selectedItem(id){
					data.moren=id
					// console.log('当前默认'+id)
					let shangciid=state.shopcart.yongyaoren.yyr_id
					// console.log('上次默认'+dangqianmoren)
					// 将当前默认的用户信息存起来【修改仓库信息】
					let cf_id=state.shopcart.chufangArr.cf_id
					let cf_yongyaorenid
					for (let i = 0; i < data.itemData.length; i++) {
						if(data.itemData[i].yyr_id==shangciid){
							data.itemData[i].yyr_isUsed=0
						}
						if(data.itemData[i].yyr_id==id){
							state.shopcart.yongyaoren=data.itemData[i]
							data.itemData[i].yyr_isUsed=1
							cf_yongyaorenid=data.itemData[i].yyr_id
						}
					}
					// 【修改数据库信息】
					http({
						url:'/changemorenyyr',
						method:'post',
						data:{
							id:id,
							shangciid:shangciid,
							cf_id:cf_id,
							cf_yongyaorenid:cf_yongyaorenid
						}
					}).then((res)=>{
						console.log(res)
						if(res.data.code==200){
							// 重新设置仓库的值
							let id=1
							// 查询该用户的处方以及用药人信息
							http({
								url:'/getchufangxinxi',
								method:'post',
								data:{
									id:id
								}
							}).then((res)=>{
								// console.log(res.data.data)
								if(res.data.code==200){
									commit('shopcart/getallchufangandyyrData',res.data.data)
									console.log(state.shopcart.allchufangandyyrArr)
									for (let i = 0; i < state.shopcart.allchufangandyyrArr.length; i++) {
										if(state.shopcart.allchufangandyyrArr[i].cf_yonyaorenId==state.shopcart.yongyaoren.yyr_id &&state.shopcart.allchufangandyyrArr[i].cf_isshiyong==1){
											commit('shopcart/getchufangData',state.shopcart.allchufangandyyrArr[i])
										}
									}
								}
							})
						}
					})
				}
			})
			
			onMounted(()=>{
				console.log(data.itemData)
				// console.log(state.shopcart.chufangArr)
				for (let i = 0; i < data.itemData.length; i++) {
					if(data.itemData[i].yyr_isUsed==1)data.moren=data.itemData[i].yyr_id
				}
				
			})
			return {...toRefs(data)}
		},
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style lang="scss">
	@import url("~@/static/fonts/iconfont.css");
	.header{
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		border-bottom: 1rpx solid #efefef;
		display: flex;
		justify-content: space-between;
		background-color: white;
		>text:nth-child(2){
			color: red;
		}
	}
	.item{
		padding: 0rpx 10rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		background-color: #efefef;
	}
	.cFirst{
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: white;
		view:nth-child(1){
			flex: 1;
			view:not(:first-child){
				margin-top: 20rpx;
			}
		}
	}
	.icon-yishiming1-copy{
		// color: red;
	}
	.icon-xinyongbaogao-benren-copy{
		color: red;
		font-size: 30rpx;
	}
	.icon-jianqu{
		color: red;
	}
	.btn{
		text-align: center;
		margin-top: 40rpx;
		button{
			width: 370rpx;
			font-size: 16px;
		}
	}
</style>
